<style type="text/css">
    .action-cell {
        margin-bottom: 20px;
        width: 100%;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: distribute;
        justify-content: space-between;
        align-items: center;
        margin-right: 20%;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div>
        <h4>车辆类型管理</h4>
        <Alert show-icon>
            此处用于显示的车辆管理。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="addIsShow=true">
                <Icon type="md-add-circle" />
                添加
            </Button>
            <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                <Icon type="md-close-circle" />
                删除
            </Button>
        </ButtonGroup>
        <Row>
            <Col span="9">
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked" @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>

        <Modal v-model="addIsShow" title="添加" @on-ok="handleSubmit('formValidate')"
               @on-cancel="addIsShow =false">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
                <Form-item label="车辆类型名称" prop="carType">
                    <i-input v-model="formValidate.carType" placeholder="请输入车辆类型"></i-input>
                </Form-item>
            </Form>
        </Modal>
        <Modal v-model="addIsShow2" title="添加" @on-ok="handleSubmitEdit('formValidate2')"
               @on-cancel="addIsShow2 =false">
            <Form ref="formValidate2" :model="formValidate2" :rules="ruleValidate2" :label-width="100">
                <Form-item label="车辆类型名称" prop="title">
                    <i-input v-model="formValidate2.title" placeholder="请输入车辆类型"></i-input>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                tableData: [],
                //操作按钮状态管理
                deleteBtnEnable: true,
                //添加
                formValidate: {
                    //添加字段
                    carType: '',
                },
                addIsShow: false,
                ruleValidate: {},
                //编辑
                formValidate2: {
                    //添加字段
                    carType: '',
                },
                addIsShow2: false,
                ruleValidate2: {},
                selectedItems: [],
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                    },
                    {
                        title: '车辆类型',
                        key: 'Title',
                    },
                ],
            }
        },
        mounted() {
            this.mockTableData();
        },
        components: {},
        methods: {
            handleSubmit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPS(model);
                })
            },
            comfirmModifyPS: function (model) {
                let that = this;
                let params = that.$qs.stringify(model);
                apiUtil.post(that, apiUtil.urls.AddCarType, params, function (res) {
                    that.mockTableData();
                });
            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:', selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ');
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //跳转
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id,
                            value: ''
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            //列表总数据
            mockTableData() {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.CarTypesList, params, function (res) {
                    that.$Loading.finish();
                    that.tableData = res.Msg;
                    console.log('res:', res)
                });

            },
            //删除
            remove(index) {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的项，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for(var i = 0; i<that.selectedItems.length; i++){
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);
                        apiUtil.post(that, apiUtil.urls.DelCarType, params, function (res) {
                            that.mockTableData();
                        });
                    }
                });
            },
        },
    }
</script>
